package com.example.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @Description : 单例模式：饱汉
 * @Author : Bruce Lee
 * @CreateTime : 2025/3/13
 */

@Slf4j
public class SaturationSingleton {

    private HashMap<String, Integer> map = new HashMap<>();

    private static SaturationSingleton saturationSingleton;

    private SaturationSingleton() {
    }

    public static SaturationSingleton getSaturationSingleton(){
        if(saturationSingleton != null){
            return saturationSingleton;
        }else{
            synchronized (SaturationSingleton.class){
                // 多线程情况下，锁释放可能会导致另一个线程进入该逻辑
                if(saturationSingleton != null){
                    return saturationSingleton;
                }
                saturationSingleton = new SaturationSingleton();
                return saturationSingleton;
            }
        }
    }

    public void add(String key, Integer value){
        map.put(key, value);
        log.info("add key:{} value:{}", key, value);
    }

    public void remove(String key){
        map.remove(key);
        log.info("remove key:{}", key);
    }

    public HashMap<String, Integer> getMap(){
        return map;
    }


}
